package org.budo.xml;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Xml节点基类
 * 
 * @author lmw
 */
public abstract class AbstractXmlObject implements XmlObject {
    public XmlObject getChildNodeByName(Node node, String nodeName) {
        NodeList childNodes = node.getChildNodes();

        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (nodeName.equals(child.getNodeName())) {
                return new XmlNodeObject(child);
            }
        }

        return null;
    }

    public XmlObject[] getChildNodeArrayByName(Node node, String nodeName) {
        List<XmlObject> xmlObjects = new ArrayList<XmlObject>();

        NodeList childNodes = node.getChildNodes();

        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (nodeName.equals(child.getNodeName())) {
                xmlObjects.add(new XmlNodeObject(child));
            }
        }

        return xmlObjects.toArray(new XmlObject[0]);
    }

    @Override
    public String getAttributeValue(String attributeName) {
        throw new RuntimeException();
    }

    @Override
    public String getTextContent() {
        throw new RuntimeException();
    }

    @Override
    public String getChildNodeTextContentByName(String nodeName) {
        throw new RuntimeException();
    }
}